package com.cgtong.android.widget;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;

import com.cgtong.android.R;


/**
 * Created by cc
 * on 2017/8/16.
 */
public abstract class BaseDialogFragment extends DialogFragment {

  private static final String TAG = "base_dialog_fragment";
  private static final float DEFAULT_DIM = 0.2F;

  public BaseDialogFragment() {
  }

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setStyle(DialogFragment.STYLE_NO_TITLE,this.getStyle());
  }

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    this.getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    this.getDialog().setCanceledOnTouchOutside(this.getCancelOutSide());
    this.getDialog().setCancelable(this.getCanCancelable());
    View view = inflater.inflate(this.getLayoutRes(), container, false);
    this.bindView(view, this.getDialog());
    return view;
  }


  @LayoutRes
  public abstract int getLayoutRes();

  public abstract void bindView(View view, Dialog dialog);

  @Override
  public void onStart() {
    super.onStart();
    
    //=============
    Window window = getDialog().getWindow();
    WindowManager.LayoutParams params = window.getAttributes();

    params.dimAmount = getDimAmount();
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    if (getHeight() > 0) {
      params.height = getHeight();
    } else {
      params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    }
    params.gravity = this.getGravity();

    window.setAttributes(params);
  }

  public int getHeight() {
    return LayoutParams.MATCH_PARENT;
  }

  public float getDimAmount() {
    return DEFAULT_DIM;
  }

  public int getGravity() {
    return Gravity.BOTTOM;
  }


  public boolean getCancelOutSide() {
    return true;
  }
  
  public boolean getCanCancelable(){
    return false;
  }

  public int getStyle(){
    return  R.style.DefaultDialog;
  }
  
  
  
  public String getFragmentTag() {
    return TAG;
  }

  public void show(FragmentManager fragmentManager) {
    this.show(fragmentManager, this.getFragmentTag());
  }
}
